package com.health.mapp.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.UserCardIssue;
import com.health.db.service.user.IUserCardListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.ActiveCardCourseReq;
import com.health.mapp.bean.ActiveCardCourseResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 课程激活接口
 * 充值卡 健康卡 会员卡
 * @author HiRoad
 *
 */
public class ActiveCardCourseImpl  implements ActionInterface {

	private static Logger logger = Logger.getLogger(ActiveCardCourseImpl.class);
	
	@Autowired
	private IUserLISTService userLISTService;
	@Autowired
	private IUserCardListService userCardListService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		ActiveCardCourseReq req=(ActiveCardCourseReq) reqBean;
		ActiveCardCourseResp resp = new ActiveCardCourseResp();
		//2为充值卡
		String lable="课程";
		
		if(StringUtils.isBlank(req.getUserId())||StringUtils.isBlank(req.getCardCode())){
			resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return resp;
		}
		UserCardIssue issue = userCardListService.selectUserCardIssueByPasword(req.getCardCode());
		
		if (null==issue) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3009);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3009_NOTE);
			return resp;
		}else if (null!=issue.getOwnerid()) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3011);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3011_NOTE);
			return resp;
		}else if (null==issue.getUserCardType()) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3010);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3010_NOTE);
			return resp;
		}
		
		int returnFlg= userCardListService.activeCourseCard(Long.valueOf(req.getUserId()), issue);
		
		if (returnFlg==-3) {
			resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3017);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ORDER_ERROR_3017_NOTE);
		}else{
			resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE__3012_NOTE);
		}
		logger.info(ResultCode.OPERATION_SUCCESS);
		
		return resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the userCardListService
	 */
	public IUserCardListService getUserCardListService() {
		return userCardListService;
	}
	/**
	 * @param userCardListService the userCardListService to set
	 */
	public void setUserCardListService(IUserCardListService userCardListService) {
		this.userCardListService = userCardListService;
	}
	
	
	
	
	
}
